﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class user_list : System.Web.UI.Page
    {
        VipInfoBLL bll = new VipInfoBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetVipInfo();
            }
          
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //获取VIP用户信息
                string Id = this.txt_id.Text.Trim();
                string card = this.txt_vipcard.Text.Trim();
                string name = this.txt_name.Text.Trim();
                int sex =Convert.ToInt32( this.rbl_sex.SelectedValue);
                string phone = this.txt_phone.Text.Trim();
                string address = this.txt_adress.Text.Trim(); 
                //string registertime = this.txt_time.Text.Trim();
                string grade = this.txt_grade.Text.Trim();

                //实例化业务逻辑层
                
                VipInfoModel model = new VipInfoModel();
                model.vipId = Convert.ToInt32(Id);
                model.vipcard = Convert.ToInt32(card);
                model.vipname = name;
                model.sex = Convert.ToBoolean(sex);
                model.phone = phone;
                model.address = address;
                model.registertime = DateTime.Now;
                model.grade = grade;
                //判断
                if (bll.AddVipInfosProc(model))
                {
                    //Response.Write("<script>alert('添加成功');location.href='user_list.aspx';</script>");
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"alert", "<script>alert('添加成功');location.href='user_list.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('添加失败');</script>");
                }

            }
        }

        protected void btn_clear_Click(object sender, EventArgs e)
        {
            
        }

        protected void lbt_del_Command(object sender, CommandEventArgs e)
        {
            //判断是否是点击删除命令
            if (e.CommandName == "del")
            {
                int commodityID = Convert.ToInt32(e.CommandArgument);
                if (bll.DeleteVipInfosoProc(commodityID))
                {
                    Response.Write("<script>alert('删除成功');location.href='user_list.aspx'</script>");
                    //BindUserInfo();
                }
                else
                {
                    Response.Write("<script>alert('删除失败')</script>");
                }
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            GetVipInfo();
        }

        protected void GetVipInfo()
        {
            //DataTable list = bll.GetVipInfos();
            //获取检索的信息
            string card = txt_search_card.Text.Trim();
            string name = txt_search_name.Text.Trim();
            int sex = Convert.ToInt32(rbl_search_sex.SelectedValue);

            DateTime? StartRegistertime = null;
            if (!string.IsNullOrWhiteSpace(txt_StartRegistertime.Value))
            {
                StartRegistertime = Convert.ToDateTime(txt_StartRegistertime.Value.Trim());
            }

            DateTime? EndRegistertime = null;
            if (!string.IsNullOrWhiteSpace(txt_EndRegistertime.Value))
            {
                EndRegistertime = Convert.ToDateTime(txt_EndRegistertime.Value.Trim());
            }


            //类的初始化器
            VipInfoSearchModel model = new VipInfoSearchModel()
            {
                vipcard = card,
                vipname = name,
                sex = sex,
                StartRegistertime=StartRegistertime,
                EndRegistertime=EndRegistertime
            };
            int totalCount;
            List<VipInfoModel> list = bll.GetVipInfosProc(model,Vip_Pager.CurrentPageIndex,Vip_Pager.PageSize,out totalCount);

            Vip_Pager.RecordCount = totalCount;

            this.rpt_VipInfo.DataSource = list;
            this.rpt_VipInfo.DataBind();
        }

        protected void btn_search_Click1(object sender, EventArgs e)
        {
            GetVipInfo();
        }

        protected void Vip_Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            Vip_Pager.CurrentPageIndex = e.NewPageIndex;
            GetVipInfo();
        }
    }
}